package com.arina.onepagesmarthome;

import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;

public class getGlobalDataFromXML {
	public static globaldata readXML(Context context,String assetXMLFileName)
	{
		XmlPullParserFactory pullParserFactory;
		try {
			pullParserFactory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = pullParserFactory.newPullParser();

			InputStream in_s = context.getAssets().open(assetXMLFileName);
			parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
			parser.setInput(in_s, null);

			return parseXML(parser);

		} catch (XmlPullParserException e) {

			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private static globaldata parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
	{
		globaldata data = new globaldata();
		int eventType = parser.getEventType();
		int index=0;
		int indexSSID=0;
		while (eventType != XmlPullParser.END_DOCUMENT)
		{
			String name = null;

			if( eventType==XmlPullParser.START_TAG)
			{
				name = parser.getName();
				if (name.equalsIgnoreCase("name"))
				{
					data.homename=parser.nextText();
				} 
				if (name.equalsIgnoreCase("rooms")){
					int numRooms=Integer.parseInt(parser.getAttributeValue(null, "numberLayout"));
					data.rooms=new room[numRooms];
				}
				if(name.equalsIgnoreCase("room"))
				{
					String layoutName=parser.getAttributeValue(null, "layout");
					String layoutTitle=parser.nextText();
					room temproom=new room(layoutName, layoutTitle);
					data.rooms[index]=temproom;
					index++;
				}
				if(name.equalsIgnoreCase("houseowner"))
				{
					data.homeowner=parser.getAttributeValue(null, "name");
				}
				if(name.equalsIgnoreCase("ssids"))
				{
					int numSSID=Integer.parseInt(parser.getAttributeValue(null, "numberSSID"));
					data.ssid=new String[numSSID];
				}
				if(name.equalsIgnoreCase("ssid"))
				{
					data.ssid[indexSSID]=parser.getAttributeValue(null, "name");
				}
				if(name.equalsIgnoreCase("system"))
				{
//					String systemtype=parser.getAttributeValue(null, "type");
//					if(systemtype.equalsIgnoreCase("sbus"))
//						data.systemtype=SYSTEM_TYPE.SBUS;
//					else
//						data.systemtype=SYSTEM_TYPE.ARINA;
//					data.localIP=parser.getAttributeValue(null, "ip");
//					data.ddns=parser.getAttributeValue(null, "ddns");
//					data.localPort=Integer.parseInt(parser.getAttributeValue(null, "controlport"));
//					data.broadcastPort=Integer.parseInt(parser.getAttributeValue(null, "broadcastport"));
				}

			}
			eventType = parser.next();
		}
		return data;
	}
}
